package com.juku2024.juku.viewmodel

import android.text.TextUtils
import androidx.lifecycle.viewModelScope
import com.juku2024.juku.model.*
import com.juku2024.juku.network.api.Api
import com.juku2024.juku.network.retrofit.RetrofitUtils
import com.juku2024.juku.tools.AppUtils
import com.juku2024.juku.tools.Constants
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class FeedbackViewModel : BaseViewModel() {


    fun fetchFeedback(success: (result: FeedbackInfoModel) -> Unit, fail: (msg: String) -> Unit) {
        doBaseHttp({
            return@doBaseHttp RetrofitUtils.instance.getApi(Api::class.java)
                .fetchFeedbackInfo(AppUtils.getDeviceId(),
                    AppUtils.PLATFORM,
                    System.currentTimeMillis(),
                    AppUtils.getChannelValue())
        }, {
            success(it)
        }, fail)
    }

    fun requestSendSystemMsgRead(sysmsg: SysMsg, success: (result: FeedbackInfoModel) -> Unit, fail: (msg: String) -> Unit) {
        doBaseHttp({
            val params = HashMap<String, String>()
            params["device_id"] = AppUtils.getDeviceId()
            params["platform"] = AppUtils.PLATFORM
            params["version_number"] = AppUtils.getVersionCode().toString()
            params["version_code"] = AppUtils.getVersionName()
            params["msg"] = sysmsg.content!!
            params["channel"] = AppUtils.getChannelValue()
            params["action_date"] = System.currentTimeMillis().toString()
            params["news_id"] = sysmsg.id.toString()
            return@doBaseHttp RetrofitUtils.instance.getApi(Api::class.java).postFeedback(params)
        }, {
            success(it)
        }, fail)
    }

    fun postFeedback(text: String, success: (result: FeedbackInfo) -> Unit, fail: (msg: String) -> Unit) {
        doBaseHttp({
            val params = HashMap<String, String>()
            params["device_id"] = AppUtils.getDeviceId()
            params["platform"] = AppUtils.PLATFORM
            params["version_number"] = AppUtils.getVersionCode().toString()
            params["version_code"] = AppUtils.getVersionName()
            params["msg"] = text
            params["channel"] = AppUtils.getChannelValue()
            params["action_date"] = System.currentTimeMillis().toString()
            return@doBaseHttp RetrofitUtils.instance.getApi(Api::class.java).postFeedback(params)
        }, {
            //反馈成功
            val feedbackInfo = FeedbackInfo()
            feedbackInfo.msg = text
            feedbackInfo.actionDate = System.currentTimeMillis() / 1000L
            feedbackInfo.roleId = Constants.ROLE_ME
            success(feedbackInfo)
        }, fail)
    }


    /**
     * 查询是否有新的系统消息
     */
    fun hasNewSystemMsg(complete: (hasNewMsg: Boolean) -> Unit){
        val deviceId = AppUtils.getDeviceId()
        if (TextUtils.isEmpty(deviceId)) return
        viewModelScope.launch {
            try {
                delay(3000)
                val url = RetrofitUtils.CHECK_SYSTEM_MSG_URL
                val result = withContext(Dispatchers.IO) {
                    RetrofitUtils.instance.getApi(Api::class.java).hasNewMsg(url, deviceId, AppUtils.getChannelValue())
                }
                val hasNewSystemMsg = result?.data?.hasNewMsg?:false
                complete(hasNewSystemMsg)
            } catch (e: Exception) {
                e.printStackTrace()
                complete(false)
            }
        }
    }

}